#include "hosyntaxhighlighter.h"

HoSyntaxHighlighter::HoSyntaxHighlighter(QStringList keywordPatterns, QTextDocument *parent) : QSyntaxHighlighter(parent) {
    QTextCharFormat keywordFormat;
    keywordFormat.setForeground(Qt::blue);
    keywordFormat.setFontWeight(QFont::Bold);

    for (const QString &pattern : keywordPatterns) {
        HighlightingRule rule;
        rule.pattern = QRegularExpression(pattern);
        rule.format = keywordFormat;
        this->highlightingRules.append(rule);
    }
}

void HoSyntaxHighlighter::highlightBlock(const QString &text) {
    for (const HighlightingRule &rule : this->highlightingRules) {
        QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
        while (matchIterator.hasNext()) {
            QRegularExpressionMatch match = matchIterator.next();
            setFormat(match.capturedStart(), match.capturedLength(), rule.format);
        }
    }
}
